package org.cuit.seon.service;

import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.cuit.seon.core.context.ApiConfiguration;
import org.cuit.seon.core.data.PageQuery;
import org.cuit.seon.core.data.PageUtils;
import org.cuit.seon.core.data.R;
import org.cuit.seon.core.response.Brand;
import org.cuit.seon.core.response.BrandDTO;

import java.util.HashMap;
import java.util.Map;


public class ApiService {
    private final RequestExecutor executor;

    public ApiService(ApiConfiguration config) {
        executor = new RequestExecutor(config);
    }

    public Map<String, Integer> getTokenMap() {
        return executor.getTokenMap();
    }

    /**
     * get对接
     */
    public R<PageUtils<Brand>> getBrandPage(PageQuery pageQuery, String keyword) {
        Map<String, String> query = new HashMap<>();
        query.put("pn", pageQuery.getPn() == null ? null : String.valueOf(pageQuery.getPn()));
        query.put("ps", pageQuery.getPs() == null ? null : String.valueOf(pageQuery.getPs()));
        query.put("offset", pageQuery.getOffset() == null ? null : String.valueOf(pageQuery.getOffset()));
        query.put("name", StringUtils.isBlank(keyword) ? null : keyword);
        return executor.get("/brand/page", query, new TypeToken<PageUtils<Brand>>() {
        }.getType());
    }

    /**
     * post对接
     */
    public R<BrandDTO> testPost(BrandDTO brandDTO) {
        return executor.post("/brand/testPost", null, brandDTO, new TypeToken<BrandDTO>() {
        }.getType());
    }

}
